package com.oujiong.redisson.config;

import org.redisson.Redisson;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;

import com.oujiong.redisson.RedissonLock;
import com.oujiong.redisson.RedissonManager;

import lombok.extern.slf4j.Slf4j;

/**
 * Redisson自动化配置
 */
@Configuration
@ConditionalOnClass(Redisson.class)
@Slf4j
public class RedissonAutoConfiguration {

	@Bean
	@ConditionalOnMissingBean
	@Order(value = 2)
	public RedissonLock redissonLock(final RedissonManager redissonManager) {
		final RedissonLock redissonLock = new RedissonLock(redissonManager);
		RedissonAutoConfiguration.log.info("[RedissonLock]组装完毕");
		return redissonLock;
	}

	@Bean
	@ConditionalOnMissingBean
	@Order(value = 1)
	public RedissonManager redissonManager(final RedissonConfig redissonProperties) {
		final RedissonManager redissonManager = new RedissonManager(redissonProperties);
		RedissonAutoConfiguration.log.info("[RedissonManager]组装完毕,当前连接方式:" + redissonProperties.getType() + ",连接地址:"
				+ redissonProperties.getAddress());
		return redissonManager;
	}
}
